<script setup lang="ts">
import { ref, onMounted } from 'vue'
import SearchItem from '../components/search/index.vue'
import type { RoleListResponseData, RoleList, Role } from '@/apis/acl/role/type'
import { getRoleListService, deleteRoleService } from '@/apis/acl/role'
import AddEditRole from './components/addeditrole/index.vue'
import SetPermission from './components/setpermission/index.vue'
// 搜索
const keywords = ref<string>('')

const goSearch = (val: string) => {
  if (val === 'search') {
    getRoleList()
  } else {
    keywords.value = ''
    getRoleList()
  }
}
// 分页器
const pageNo = ref<number>(1)
const pageSize = ref<number>(5)
const total = ref<number>(0)
// 角色列表
const roleList = ref<RoleList>([])

// 获取角色列表
const getRoleList = async (pager = 1) => {
  pageNo.value = pager
  const res: RoleListResponseData = await getRoleListService(
    pageNo.value,
    pageSize.value,
    keywords.value
  )
  roleList.value = res.data.records
  total.value = res.data.total
}

// 分页器size变化
const onSizeChange = () => {
  getRoleList()
}

// 渲染页面
onMounted(() => {
  getRoleList()
})
// 添加职位、修改职位
const addEditRef = ref()
const addRole = () => {
  addEditRef.value.open({})
}

const editRole = (row: Role) => {
  addEditRef.value.open(row)
}

const addEditRole = (val: string) => {
  if (val === 'add') {
    getRoleList()
    ElMessage.success('添加成功')
  } else {
    getRoleList(pageNo.value)
    ElMessage.success('修改成功')
  }
}

// 分配权限
const setPermissionRef = ref()
const onAssignPermission = (row: Role) => {
  setPermissionRef.value.open(row)
}

// 删除职位
const deleteRole = async (id: string) => {
  await deleteRoleService(id)
  getRoleList(roleList.value.length === 1 ? pageNo.value - 1 : pageNo.value)
  ElMessage.success('删除成功')
}

//
</script>

<template>
  <SearchItem v-model="keywords" @search="goSearch"></SearchItem>

  <el-card style="margin: 10px 0">
    <el-button type="primary" @click="addRole" icon="Plus">添加职位</el-button>

    <el-table border style="margin: 10px 0" :data="roleList">
      <el-table-column label="#" type="index" align="center"></el-table-column>
      <el-table-column label="id" prop="id" align="center"></el-table-column>
      <el-table-column
        label="职位名称"
        prop="roleName"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="创建时间"
        prop="createTime"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="更新时间"
        prop="updateTime"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="操作" width="260px">
        <template #default="{ row }">
          <el-button
            type="success"
            round
            icon="User"
            @click="onAssignPermission(row)"
            >分配权限</el-button
          >
          <el-button
            type="warning"
            circle
            icon="Edit"
            @click="editRole(row)"
          ></el-button>
          <el-popconfirm
            title="你确定删除吗？"
            icon="Delete"
            @confirm="deleteRole(row.id)"
          >
            <template #reference>
              <el-button type="danger" circle icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="暂无数据"></el-empty>
      </template>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      layout="prev, pager, next, jumper, ->, total, sizes"
      :total="total"
      background
      :page-sizes="[5, 10, 15]"
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      @current-change="getRoleList"
      @size-change="onSizeChange"
    >
    </el-pagination>
  </el-card>

  <!-- 添加、修改职位 -->
  <AddEditRole ref="addEditRef" @addRole="addEditRole"></AddEditRole>

  <!-- 分配权限 -->
  <SetPermission ref="setPermissionRef"></SetPermission>
</template>
